/*
 * Copyright (C) 2016  Nicola Spanti (RyDroid) <dev@nicola-spanti.info>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


#include "PlanetWars2dRT-SDL2/specific/view/draw_game_state.h"
#include "PlanetWars2dRT-SDL2/utils/SDL.h"
#include "PlanetWars2dRT-core/utils/color_rgb_const.h"
#include <SDL2_gfxPrimitives.h>


EXTERN_C_BEGIN


bool
SDL_draw_selection_started_rectangle(SDL_Renderer*            renderer,
				     const selection_2d_uint* selection,
				     FILE*                    error_stream)
{
  bool result =
    boxRGBA(renderer,
	    RECTANGLE_GET_TOP_LEFT_POSITION_X    (selection->zone.rectangle),
	    RECTANGLE_GET_TOP_LEFT_POSITION_Y    (selection->zone.rectangle),
	    RECTANGLE_GET_BOTTOM_RIGHT_POSITION_X(selection->zone.rectangle),
	    RECTANGLE_GET_BOTTOM_RIGHT_POSITION_Y(selection->zone.rectangle),
	    COLOR_RGB_WHITE.red,
	    COLOR_RGB_WHITE.green,
	    COLOR_RGB_WHITE.blue,
	    SELECTION_COLOR_ALPHA_VALUE) == 0;
  if(!result)
    {
      fprintf(error_stream,
	      "The selection (of type rectangle) was not rendered!\n");
      SDL_print_error();
      return false;
    }
  return true;
}

bool
SDL_draw_selection(SDL_Renderer*            renderer,
		   const selection_2d_uint* selection,
		   FILE*                    error_stream)
{
  if(selection == NULL)
    {
      fprintf(error_stream,
	      "The selection was not rendered (nullptr)!\n");
      return false;
    }
  if(selection->state == SELECTION_STATE_NULL)
    {
      return true;
    }
  
  if(selection->type == SELECTION_2D_TYPE_RECTANGLE)
    {
      return SDL_draw_selection_started_rectangle(renderer,
						  selection,
						  error_stream);
    }
  fprintf(error_stream,
	  "The selection was not rendered (unknow type)!\n");
  return false;
}

bool
SDL_draw_game_state(SDL_Renderer*       renderer,
		    const game_state_t* a_game_state,
		    FILE*               error_stream)
{
  return SDL_draw_selection(renderer,
			    &a_game_state->selection.selection,
			    error_stream);
}


EXTERN_C_END
